package at.ac.tuwien.law.yaplaf.hibernate;

import org.hibernate.*;
import org.hibernate.cfg.*;

public class HibernateFactory {
	 private static final SessionFactory sessionFactory;
	
	    static {
	        try {
	        	
	            // Create the SessionFactory from hibernate.cfg.xml
	            sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
	          
	            
	        } catch (Throwable ex) {
	            // Make sure you log the exception, as it might be swallowed
	            System.err.println("Initial SessionFactory creation failed." + ex);
	            throw new ExceptionInInitializerError(ex);
	        }
	    }

	    public static Session getCurrentSession(){
	    	return sessionFactory.getCurrentSession();
	    }
	    
	    public static Session openSession(){
	    	return sessionFactory.openSession();
	    }
	    
	    public static void beginnTransaction(){
	    	sessionFactory.getCurrentSession().beginTransaction();
	    }

	    public static void endTransaction(){
	    	sessionFactory.getCurrentSession().getTransaction().commit();
	    	sessionFactory.getCurrentSession().close();
	    }
}
